<?php
/*******************************************************************************
 * $Date:: Jan 10, 2011 10:41:16 PM                                            $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage Table
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

class Lua_Table_Base extends Lua_Table_Abstract
{
    protected $_name = "";
    
    public function __construct($tableName = '', $autoFillTimestamps = false)
    {
        if ('' != $tableName) {
            $this->setTableName($tableName);
        }
        $this->autoFillTimestamps = $autoFillTimestamps;
        parent::__construct();
    }

    /** 
     * Insert data to tableName
     * @param array|Lua_Bean_Abstract $data
     * @param string $tableName
     * @return int The primary key of inserted row
     */
    public function insert($tableName = '', $data)
    {
        if ($data instanceof Lua_Bean_Abstract) {
            $tableName = $data->getTableName();
            $data = $data->toArray();
        }
        if ('' != $tableName) {
            $this->setTableName($tableName);
        }

        $this->preExecute();
        $result = parent::insert($data);
        $this->postExecute();
        return $result;
    }
    
    /** 
     * Update data
     * @param array $data
     * @param array|string|Zend_Db_Select $where
     * @param string $tableName
     * @return int The number of effectived rows
     */
    public function update($tableName = '', $data, $where = null)
    {
        if ('' != $tableName) {
            $this->setTableName($tableName);
        }
        $this->preExecute();
        $result =  parent::update($data, $where);
        $this->postExecute();
        return $result;
    }
    
    /**
     * Insert or update bean
     * @param Lua_Bean_Abstract $bean
     * @return int
     */
    public function save(Lua_Bean_Abstract $bean)
    {
        $tableName = $bean->getTableName();
        if (isset($bean->id) && !empty($bean->id)) {
            $result = $this->update($tableName, $bean->toArray(), array('id = ?' => $bean->id));
        } else {
            $id = $this->insert($tableName, $bean);
            $result = $bean['id'] = $id;
        }
        return $result;
    }
    
    /** 
     * Fetch row from database and bind to bean
     * @param array|string|Zend_Db_Select $where
     * @param string $tableName
     * @return Lua_Bean_Abstract
     */
    public function findRow($tableName = '', $where = null)
    {
        if ('' != $tableName) {
            $this->setTableName($tableName);
        }
        $this->preExecute();
        $bean = $this->buildBean(parent::fetchRow($where));
        $this->postExecute();
        return $bean;
    }
    
    /** 
     * Fetch all data
     * @param array|string|Zend_Db_Select $where
     * @param string $tableName
     * @param string|array $order
     * @param int $count
     * @param int $offset
     * @return array of data
     */
    public function findAll($tableName = '', $where = null, $order = null, $count = null, $offset = null)
    {
        if ('' != $tableName) {
            $this->setTableName($tableName);
        }
        $this->preExecute();
        $rows = parent::fetchAll($where, $order, $count, $offset);
        $this->postExecute();
        if (0 == $rows->count()) {
            return array();
        } else {
            return $rows->toArray();
        }
    }

    /**
     * @param  array|string $where
     * @param  string       $tableName
     * @return int
     */
    public function delete($tableName = '', $where = null)
    {
        if ('' != $tableName) {
            $this->setTableName($tableName);
        }
        $this->preExecute();
        $result = parent::delete($where);
        $this->postExecute();
        return $result;
    }

    protected function preExecute()
    {
        if ("" == $this->getTableName() || get_class($this) == $this->getTableName()) {
            throw new Lua_Table_Exception("Please specific table name before execute query");            
        }
    }

    protected function postExecute()
    {
        
    }
}